<?php namespace Admin\Controller; 

use Hdphp\Controller\Controller;

//测试控制器
class GoodsController extends CommonController{
	private $model=null;
	//构造方法
	public function __init(){
	    parent::__init();
		if(is_null($this->model)){
			$this->model=new \Common\Model\Goods;			
		}
	}
	
	
	
	public function index(){
		//获得说有商品信息
//		获得所有的数据的总数
		$total=$this->model->count();	
		$page=Page::row(10)->make($total);
		View::with('page',$page);
		$goodData=$this->model->limit(Page::limit())->get();
		View::with('goodData',$goodData);
	    View::make();
	}
	//添加商品
	public function add(){
		if(IS_POST){
			if($this->model->store()) View::success('商品添加成功',U('index'));
			View::error($this->model->geterror());
		}
				
		//获得旧数据
		//1.获得分类信息
		$oldgcateModel=new \Common\Model\Gcate;
		$oldgcateData=$oldgcateModel->get();
		$oldgcateData=Data::tree($oldgcateData,'cname','gcid');
		//2.获得品牌
		$oldbrandModel=new \Common\Model\Brand;
		$oldbrandData=$oldbrandModel->get();		
		View::with('oldbrandData',$oldbrandData);
		View::with('oldgcateData',$oldgcateData);

	    View::make();
	}
	
	//商品的删除
	public function del(){
//		获得对应的商品的gid
		$gid=Q('get.gid',0,'intval');
		//1.删除商品表单的数据
		//1)删除列表图
		$listimg=$this->model->where('gid='.$gid)->pluck('listimg');
		unlink($listimg);
		$this->model->where('gid='.$gid)->delete();
		//2.删除商品详细表的数据
		$gooddeModel=new \Common\Model\Gooddetail;
		//1).获得gdimg，并且删除掉上传的图片
		$gdimg=$gooddeModel->where('goods_gid='.$gid)->pluck('gdimg');
		$gdimg=explode(',', $gdimg);
		foreach ($gdimg as  $v) {
			if(is_file($v))unlink($v);
			
		}
		//2).删除商品详细
		$gooddeModel->where('goods_gid='.$gid)->delete();
		//3.删除商品属性表
		$gattrModel=new \Common\Model\Goodsattr;
//		删除对应gid的商品属性
		$gattrModel->where('goods_gid='.$gid)->delete();
		//删除成功提示
		View::success('删除成功',U('index'));
	}
	
	
	
	//商品的编辑
	public function edit(){
			if(IS_POST){
//				p(Q('post.'));exit;
				if($this->model->edit()) View::success('修改成功',U('goods/index'));
				View::error($this->model->geterror());
			}
		
		
		
			//获得品牌信息
			$brandData=(new \Common\Model\Brand)->get();
		 View::with('oldbrandData',$brandData);
			//获得旧的分类信息
			$gid=Q('get.gid',0,'intval');
			$oldcateData=(new \Common\Model\Gcate)->get();
			$oldcateData=Data::tree($oldcateData,'cname','gcid');
				
			//获得商品的信息
			$oldgoodData=$this->model->where('gid='.$gid)->find();
			//获得商品属性的信息(关联类型属性表)
			$goodattrModel=new \Common\Model\Goodsattr;		
			$goodsattrData=$goodattrModel->join('typeattr','taid','=','typeattr_taid')->where('goods_gid='.$gid)->get();
			//先将类型属性的tavalue转化成数组
			foreach ($goodsattrData as $k=> $v) {
				$goodsattrData[$k]['tavalue']=explode(',', $v['tavalue']);
			}
			//按照属性和规格的属性类型（也就是材质和尺码类型。0 ，1 分开）重组数组
			$gsattr=array();
			foreach ($goodsattrData as $k => $v) {
				$gsattr[$v['tatype']][]=$v;	
			}
/*******************************************************************************************************************
 * 重新组合以后的数组
Array
(
    [1] => Array
        (
            [0] => Array
                (
                    [gaid] => 21
                    [gavalue] => asd
                    [gaadd] => 4444
                    [goods_gid] => 13
                    [typeattr_taid] => 2
                    [taid] => 2
                    [taname] => dasd
                    [tavalue] => Array
                        (
                            [0] => sa
                            [1] => asd
                            [2] => asd
                            [3] => sda

                        )

                    [tatype] => 1
                    [form_fid] => 1
                )

            [1] => Array
                (
                    [gaid] => 20
                    [gavalue] => xl
                    [gaadd] => 222
                    [goods_gid] => 13
                    [typeattr_taid] => 1
                    [taid] => 1
                    [taname] => 上衣
                    [tavalue] => Array
                        (
                            [0] => x
                            [1] => xl
                            [2] => m
                            [3] => sdf

                        )

                    [tatype] => 1
                    [form_fid] => 1
                )

            [2] => Array
                (
                    [gaid] => 19
                    [gavalue] => b
                    [gaadd] => 0
                    [goods_gid] => 13
                    [typeattr_taid] => 3
                    [taid] => 3
                    [taname] => aaaaaaaa
                    [tavalue] => Array
                        (
                            [0] => aaaaaaa
                            [1] => b
                            [2] => g
                        )

                    [tatype] => 1
                    [form_fid] => 1
                )

        )
 * 

)
 * 
 * *****************************************************************************************************************
*/			
			//获得商品详情的信息
			$oldgdetail=(new \Common\Model\Gooddetail)->where('goods_gid='.$gid)->find();
			//将获得的商品详细的图册字段的所存的字符串按逗号拆分成数组
			$oldgdetail['gdimg']=explode(',',$oldgdetail['gdimg']);
			//分配数据
			View::with('oldgdetail',$oldgdetail);
			View::with('gsattr',$gsattr);
			View::with('oldgcateData',$oldcateData);
			View::with('oldgoodData',$oldgoodData);
			View::make();
	}
	
	
	
	
	
	
	
	
	
	
	
	//ajax处理
	public function ajaxdata(){		
//		获得json请求传过来的tid
		$fid=Q('post.tid',0,'intval');
		//根据tid获得所有的属性
		//1.实例化属性表
		$typeattrModel=new \Common\Model\Type;
		
		$typeattrData=$typeattrModel->where('form_fid='.$fid)->get();
		//将获的数组字段为tavalue的拆分然后分配
		$data=array();
		foreach ($typeattrData as $v) {
			
//				foreach ($v as $key => $value) {
//					if($key=='tavalue'){
//						$value=explode(',', $value)
//					}
//				}
			$arr=explode(',', $v['tavalue']);		
			$v['tavalue']=$arr;	
			$data[]=$v;	
		}
		
		echo json_encode($data);exit;
	}
	
	//upload异步上传一张图的方法
		public function upload()
	{
	    $file = Upload::path('Upload/Content/list' . date('y/m'))->make();
	    if (empty($file)) {
	        $this->ajax(Upload::getError());
	    } else {
	        /** $file内部就是以下这个数组
	            $file = array(
	                0 => array(
		                'path' => 'Upload/Content/15/8/123981239172.jpg'    ,
		                'url' => 'http://localhost/cms_edu/Upload/Content/15/8/123981239172.jpg',
		                'image' => 1
	            ),
	        );**/
	        //上传成功，把上传好的信息返给js
	        $data = $file[0];
	        echo json_encode($data);exit;
	    }
	}
	//异步上传两张图
	public function uploads()
	{
	    $file = Upload::path('Upload/Content/goods' . date('y/m'))->make();
	    if (empty($file)) {
	        $this->ajax(Upload::getError());
	    } else {
	        /** $file内部就是以下这个数组
	            $file = array(
	                0 => array(
		                'path' => 'Upload/Content/15/8/123981239172.jpg'    ,
		                'url' => 'http://localhost/cms_edu/Upload/Content/15/8/123981239172.jpg',
		                'image' => 1
	            ),
	        );**/
	        //上传成功，把上传好的信息返给js
	        $data = $file[0];
	        echo json_encode($data);exit;
	    }
	}
	
}
